package com.baosight.sczx.zm.cs.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.StaticBlocks;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.cd.Documents;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.common.zm.cInterface.cs.RoadWay;
import com.baosight.sczx.common.zm.cInterface.cs.WorkingFace;
import com.baosight.sczx.zm.cs.domain.ZMCSEngineering;

/**隶属于工程管理模块：工程信息
 * <p>Title: ServiceZMCS60.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author ZK
 * @version 1.0
 * <pre>Histroy:
 *          2016-1-11 下午04:26:34  ZK  Create
 *</pre>
 */
public class ServiceZMCSF1 extends ServiceEPBase {
	/**
	 * <p>Function: initLoad</p>
	 * <p>Description: 初始化工程信息数据</p>
	 * <p>return value: ServiceZMCS11</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-1-11下午01:11:47 ZK
	 * </p>
	 *
	*/
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的		
		inInfo.addBlock(EiConstant.resultBlock).set(EiConstant.limitStr, "100");//设置每页默认显示记录数
		EiInfo outInfo =query(inInfo);
//		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.specialFund", true));//专项资金
//		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.engineeringGroup", true));//工程组
//		outInfo.addBlock(Department.getInstance().getConstructionUnit(true));//施工部门
//		outInfo.set("engineeringNoType", SystemSettingCS.getInstance().getEngineeringNo());//工程编号形式 0自动 1手动 2生成
//		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.holeType", true));//钻孔类型
//		//		0采煤//		1掘进//		2通防//		3其他
//		if("0".equals(parentTypes)) {
//			outInfo.addBlock(WorkingFace.getAllWorkingFace("cmes.cs.engineeringParentId", true)); 
//			outInfo.addBlock(StaticBlocks.GetEngineeringClassCM(true));
//		}
//		else if("1".equals(parentTypes)) {
//			outInfo.addBlock(RoadWay.getAllRoadWay("cmes.cs.engineeringParentId", true)); 
//			outInfo.addBlock(StaticBlocks.GetEngineeringClassJJ(true));
//		}
//		else if("2".equals(parentTypes)) {
//			EiBlock _tempBlock = WorkingFace.getAllWorkingFace("cmes.cs.engineeringParentId", true);
//			_tempBlock.addRows(RoadWay.getAllRoadWay("cmes.cs.engineeringParentId", false).getRows());
//			outInfo.addBlock(_tempBlock);
//			outInfo.addBlock(StaticBlocks.GetEngineeringClassTF(true));
//		}
//		else {
//			EiBlock _tempBlock = WorkingFace.getAllWorkingFace("cmes.cs.engineeringParentId", true);
//			_tempBlock.addRows(RoadWay.getAllRoadWay("cmes.cs.engineeringParentId", false).getRows());
//			_tempBlock.addRows(OtherDesign.getAllOtherDesign("cmes.cs.engineeringParentId", false).getRows());
//			outInfo.addBlock(_tempBlock);
//			outInfo.addBlock(StaticBlocks.GetEngineeringClassQT(true));
//		}
//		outInfo.addBlock(StaticBlocks.GetEngineeringStatus(true));
//		outInfo.addBlock(StaticBlocks.GetKeyProject(true));
//		outInfo.addBlock(StaticBlocks.GetEngineeringType(true));
//		outInfo.set("fdMiningId", UserSession.getInSessionProperty("miningCode"));//矿ID的默认值
//		outInfo.set("fdMiningName", UserSession.getInSessionProperty("miningName"));//矿名称的默认值
		return outInfo;
	}
	
	/**
	 * <p>Function: query</p>
	 * <p>Description: 查询</p>
	 * <p>return value: ServiceZMCS11</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-1-11下午01:10:52 ZK
	 * </p>
	 *
	*/
	@Override
	public EiInfo query(EiInfo inInfo) {
		String parentTypes = inInfo.getString("parentTypes");// 大类
		String childTypes = inInfo.getString("childTypes");// 小类
		StaticTools.setQueryParameters(inInfo, "fdType", parentTypes, true);// 添加查询条件
		if (!(childTypes == null || "".equals(childTypes))) {
			StaticTools.setQueryParameters(inInfo, "fdClass", childTypes, true);// 添加查询条件
		}
		StaticTools.setQueryParameters(inInfo, "fdMiningId", UserSession.getInSessionProperty("miningCode"), true);
		EiInfo outInfo = super.query(inInfo, "ZMCSEngineering.query", new ZMCSEngineering());
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.specialFund", true));// 专项资金
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.engineeringGroup", true));// 工程组
		outInfo.addBlock(Department.getInstance().getMiningDepts("cmes.cp.department", "orgCode", "orgName", false, UserSession.getInSessionProperty("miningCode").toString()));// 施工部门
		outInfo.set("engineeringNoType", "1");// 工程编号形式 0自动 1手动 2生成
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.holeType", true));// 钻孔类型

		// 0采煤// 1掘进// 2通防// 3其他
		if ("0".equals(parentTypes)) {
			outInfo.addBlock(WorkingFace.getAllWorkingFace("cmes.cs.engineeringParentId", false, UserSession.getInSessionProperty("miningCode").toString()));
			outInfo.addBlock(StaticBlocks.getBlockData("cmes.cs.engineeringClass", new String[] { "综采", "炮采" }, new String[] { "综采", "炮采" }, true));// 采煤工程类型

		} else if ("1".equals(parentTypes)) {
			outInfo.addBlock(RoadWay.getAllRoadWay("cmes.cs.engineeringParentId", false, UserSession.getInSessionProperty("miningCode").toString()));
			outInfo.addBlock(StaticBlocks.getBlockData("cmes.cs.engineeringClass", new String[] { "开拓", "回采", "准备", "延伸" }, new String[] { "开拓", "回采", "准备", "延伸" }, true));// 掘进工程类型
		} else if ("2".equals(parentTypes)) {
			EiBlock _tempBlock = WorkingFace.getAllWorkingFace("cmes.cs.engineeringParentId", false, UserSession.getInSessionProperty("miningCode").toString());
			_tempBlock.addRows(RoadWay.getAllRoadWay("cmes.cs.engineeringParentId", false, UserSession.getInSessionProperty("miningCode").toString()).getRows());
			outInfo.addBlock(_tempBlock);
			outInfo.addBlock(StaticBlocks.getBlockData("cmes.cs.engineeringClass", new String[] { "打钻", "抽采", "管路安装", "水力冲孔" }, new String[] { "打钻", "抽采", "管路安装", "水力冲孔" }, true));// 通防工程类型
		} else {
			EiBlock _tempBlock = WorkingFace.getAllWorkingFace("cmes.cs.engineeringParentId", false, UserSession.getInSessionProperty("miningCode").toString());
			_tempBlock.addRows(RoadWay.getAllRoadWay("cmes.cs.engineeringParentId", false, UserSession.getInSessionProperty("miningCode").toString()).getRows());
			outInfo.addBlock(_tempBlock);
			outInfo.addBlock(StaticBlocks.getBlockData("cmes.cs.engineeringClass", new String[] { "巷修", "按拆", "零星", "喷浆", "卧底", "其他" }, new String[] { "巷修", "按拆", "零星", "喷浆", "卧底", "其他" }, true));// 其他工程类型
		}
		outInfo.addBlock(StaticBlocks.getBlockData("cmes.cs.engineeringStatus", new String[] { "0", "1", "2" }, new String[] { "设计", "开工", "竣工" }, true));// 工程状态
		outInfo.addBlock(StaticBlocks.getBlockData("cmes.cs.keyProject", new String[] { "0", "1" }, new String[] { "否", "是" }, true));// 是否重点工程
		outInfo.addBlock(StaticBlocks.getBlockData("cmes.cs.engineeringType", new String[] { "0", "1", "2", "3" }, new String[] { "采煤工程", "掘进工程", "通防工程", "其他工程" }, true));// 工程类型
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.units", true));// 计量单位
		return outInfo;
	}
	 
	
	/**
	 * <p>Function: insert</p>
	 * <p>Description: 插入</p>
	 * <p>return value: ServiceZMCS11</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-1-11下午01:10:52 张坤
	 * </p>
	 *
	*/
	@Override
	public EiInfo insert(EiInfo inInfo) {
		EiInfo out=new EiInfo(); 
		ZMCSEngineering _dom = new ZMCSEngineering();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i)); 
			String fdKeyId = TablePk.getInstance().getNextValue("T_CI_ENGINEERING");
			String fdDepartmentId = _dom.getFdDepartmentId();//施工部门ID
			String fdDepartment = Department.getInstance().getDepartmentNameById(fdDepartmentId);//施工部门
			String fdUnit=_dom.getFdUnit();
			if(fdUnit!=null){
				fdUnit=fdUnit.trim();
			}
			_dom.setFdUnit(fdUnit);
			long fdType=0;
			if(inInfo.get("parentTypes")!=null){
				fdType=Long.parseLong((String)inInfo.get("parentTypes"));//工程类型
			}
			_dom.setFdType(fdType);
			_dom.setFdKeyId(fdKeyId);//主键
			_dom.setFdDepartment(fdDepartment);
			_dom.setFdMiningId(UserSession.getInSessionProperty("miningCode").toString());
			this.dao.insert("ZMCSEngineering.insert",_dom);
			//文档 begin
//			String title = inInfo.getBlock("result").getCell(0, "fdName").toString() + "设计文档";
//			Documents.insertDoc(inInfo, fdKeyId, title, "", "", fdKeyId);//添加附件
			//end
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;  
	}

	/**
	 * <p>Function: update</p>
	 * <p>Description: 更新</p>
	 * <p>return value: ServiceZMCS11</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-1-11下午01:10:52 张坤
	 * </p>
	 *
	*/
	@Override
	public EiInfo update(EiInfo inInfo) {
		EiInfo out=new EiInfo(); 
		ZMCSEngineering _dom = new ZMCSEngineering();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i)); 
			Map<String, String> params = new HashMap<String, String>();
			params.put("fdKeyId", _dom.getFdKeyId());
			//params.put("upd", "upd");//如果开工的允许修改，放开这个注释即可
			params.put("del", "del");
			//校验竣工的工程不能再修改
/*			boolean validate = this.validate1(inInfo,_dom,  params);
			if(validate) {
				return inInfo;
			}*/
			String fdUnit=_dom.getFdUnit();
			if(fdUnit!=null){
				fdUnit=fdUnit.trim();
			}
			_dom.setFdUnit(fdUnit);
			String fdDepartmentId = _dom.getFdDepartmentId();//施工部门ID
			String fdDepartment = Department.getInstance().getDepartmentNameById(fdDepartmentId);//施工部门
			_dom.setFdDepartment(fdDepartment);
			this.dao.update("ZMCSEngineering.update",_dom);
			//文档 begin
			//String title = inInfo.getBlock("result").getCell(0, "fdName").toString() + "设计文档";
			//Documents.insertDoc(inInfo, inInfo.getBlock("result").getCellStr(0, "fdKeyId"), title, "", "", inInfo.getBlock("result").getCellStr(0, "fdKeyId"));//添加附件
			//end
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功"); 
		return out;
	}
	
	/**
	 * <p>Function: modifyStart</p>
	 * <p>Description: 开工</p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-2-15下午03:54:39 张坤
	 * </p>
	 *
	 */
	public EiInfo modifyStart(EiInfo inInfo)
	{
		ZMCSEngineering _dom = new ZMCSEngineering();
		for(int i=0; i<inInfo.getBlock("result").getRowCount(); i++)
		{
			String fdDepartmentId = inInfo.getCellStr("result",i, "fdDepartmentId");//施工部门ID
			String fdDepartment = Department.getInstance().getDepartmentNameById(fdDepartmentId);//施工部门
			inInfo.setCell("result", i, "fdDepartment", fdDepartment);
			//校验竣工的工程不能再开工
			_dom.fromMap(inInfo.getBlock("result").getRow(i)); 
			Map<String, String> params = new HashMap<String, String>();
			params.put("fdKeyId", _dom.getFdKeyId());
			params.put("upd", "upd");
/*			boolean validate = this.validate1(inInfo,_dom,  params);
			if(validate) {
				return inInfo;
			}*/
		}
		
		
		EiInfo _temp = super.update(inInfo, "ZMCSEngineering.modifyStart");
		EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 * <p>Function: modifyStop</p>
	 * <p>Description: 竣工</p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-2-15下午03:54:56 张坤
	 * </p>
	 *
	 */
	public EiInfo modifyStop(EiInfo inInfo)
	{
		EiInfo _temp = super.update(inInfo, "ZMCSEngineering.modifyStop");
		EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 * <p>Function: delete</p>
	 * <p>Description: 删除：把删除状态修改为已删除（0）</p>
	 * <p>return value: ServiceZMCS11</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-1-11下午01:10:52 张坤
	 * </p>
	 *
	*/
	@Override
	public EiInfo delete(EiInfo inInfo) {
		//删除附件信息 begin
		ZMCSEngineering _dom = new ZMCSEngineering();
		for(int i=0; i<inInfo.getBlock("result").getRowCount(); i++)
		{
			//校验竣工的工程不能再删除
			_dom.fromMap(inInfo.getBlock("result").getRow(i)); 
			Map<String, String> params = new HashMap<String, String>();
			params.put("fdKeyId", _dom.getFdKeyId());
			params.put("del", "del");
/*			boolean validate = this.validate1(inInfo,_dom,  params);
			if(validate) {
				return inInfo;
			}*/
	//		Documents.deleteDoc(inInfo.getBlock("result").getCellStr(i, "fdKeyId"));
		}
		//end
		EiInfo _temp = super.delete(inInfo, "ZMCSEngineering.delete");		
		EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 * <p>Function: validate</p>
	 * <p>Description: 校验编号、名称、简称</p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-2-15下午03:58:53 张坤
	 *    2016-7-14 16:13:04 lhx modify
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	public EiInfo validate(EiInfo inInfo) {
		ZMCSEngineering _dom = new ZMCSEngineering();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		String oper=(String)inInfo.get("efCurButtonEname");
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i)); 
			String fdNo = _dom.getFdNo();
			String fdName = _dom.getFdName();
			String fdAbbreviation = _dom.getFdAbbreviation();
			String fdMiningId=_dom.getFdMiningId();
			if(fdMiningId==null ||fdMiningId.trim().equals("")){
				fdMiningId=UserSession.getInSessionProperty("miningCode").toString();
			}
			Map<String, String> params = new HashMap<String, String>();
			if(oper.equalsIgnoreCase("update")){
				params.put("fdKeyId",_dom.getFdKeyId());
			}
			params.put("fdNo", fdNo);
			params.put("fdName", fdName);
			params.put("fdAbbreviation", fdAbbreviation);
			params.put("fdMiningId", fdMiningId);
			List result = dao.query("ZMCSEngineering.validate", params);
			if(result != null && result.size() > 0) {
				inInfo.setStatus(-1);
				ZMCSEngineering _temp = (ZMCSEngineering)result.get(0);
				
				StringBuffer msg = new StringBuffer("提交失败，");
				if(_temp.getFdNo() != null && _temp.getFdNo().equals(fdNo)) {
					msg.append(" 编号 ");
				}
				if(_temp.getFdName() != null && _temp.getFdName().equals(fdName)) {
					msg.append(" 名称 ");
				}
				if(_temp.getFdAbbreviation() != null && _temp.getFdAbbreviation().equals(fdAbbreviation)) {
					msg.append(" 简称 ");
				}
				msg.append("重复！");
				inInfo.setMsg(msg.toString());
				return inInfo;
			}
		}
		inInfo.setStatus(1);
		return inInfo;
	}
	
	/**
	 * <p>Function: validate1</p>
	 * <p>Description: 校验要开工的工程是否是竣工工程</p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-7-6 09:21:18 李红星
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	public EiInfo validate1(EiInfo inInfo) {
		ZMCSEngineering _dom = new ZMCSEngineering();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		String oper=(String)inInfo.get("efCurButtonEname");
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			Map<String, String> params = new HashMap<String, String>();
			params.put("fdKeyId", _dom.getFdKeyId());
			if(oper.equalsIgnoreCase("update")||oper.equalsIgnoreCase("delete")){
				params.put("del", "del");
			}else if(oper.equalsIgnoreCase("modifystart")){
				params.put("upd", "upd");
			}
			List result = dao.query("ZMCSEngineering.validate1", params);
			if(result != null && result.size() > 0) {
				inInfo.setStatus(-1);
				StringBuffer msg = new StringBuffer("提交失败，");
				msg.append("开工和竣工的工程不能修改或删除！竣工的工程不能再开工");
				inInfo.setMsg(msg.toString());
				return inInfo;
			}
		}
		inInfo.setStatus(1);
		return inInfo;
	}

	/**
	 * 查询附件
	 * <p>Function: queryFiles</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-6-30上午10:38:30 张坤
	 * </p>
	 *
	 */
	public EiInfo queryFiles(EiInfo inInfo) {
		String keyId = inInfo.getString("fdKeyId");
		return Documents.getFilesInfo("filesresult", keyId);
	}
}